import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
var _excluded = ["__id", "_status", "tenantName", "serverId"],
    _excluded2 = ["tenantId", "serverCode", "serverName", "serverTypeCode", "serverId", "accessSecret", "accessKey", "enabledFlag", "objectVersionNumber", "_token", "tenantName", "serverTypeMeaning", "_status", "extParam"],
    _excluded3 = ["tenantId", "serverCode", "serverName", "serverTypeCode", "serverId", "accessSecret", "accessKey", "enabledFlag", "objectVersionNumber", "_token", "tenantName", "serverTypeMeaning", "_status", "extParam"];
import { CPAAS_MSG, VERSION_IS_OP, CPAAS_PLATFORM } from 'utils/config';
import { getCurrentOrganizationId, isTenantRoleLevel } from 'utils/utils';
import intl from 'utils/intl';
import { CODE_UPPER } from 'utils/regExp';
var organizationId = getCurrentOrganizationId();

function codeValidator(value) {
  if (CODE_UPPER.test(value)) {
    return true;
  }

  return intl.get('cpaas.common.validation.codeUpper').d('全大写及数字，必须以字母、数字开头，可包含“-”、“_”、“.”、“/”');
}

var tableDs = {
  selection: false,
  autoQuery: true,
  queryFields: [!isTenantRoleLevel() && {
    name: 'tenantLov',
    type: 'object',
    label: intl.get('entity.tenant.name').d('租户名称'),
    lovCode: 'CPFM.TENANT',
    noCache: true,
    ignore: 'always'
  }, !isTenantRoleLevel() && {
    name: 'tenantId',
    type: 'string',
    bind: 'tenantLov.tenantId'
  }, {
    name: 'serverCode',
    type: 'string',
    label: intl.get('cmsg.callServer.model.callServer.serverCode').d('服务编码')
  }, {
    name: 'serverName',
    type: 'string',
    label: intl.get('cmsg.callServer.model.callServer.serverName').d('服务名称')
  }, {
    name: 'enabledFlag',
    type: 'string',
    label: intl.get('cpaas.common.status').d('状态'),
    lookupCode: 'CPFM.ENABLED_FLAG'
  }].filter(Boolean),
  fields: [{
    name: 'tenantName',
    type: 'string',
    label: intl.get('entity.tenant.name').d('租户名称')
  }, {
    name: 'serverCode',
    type: 'string',
    label: intl.get('cmsg.callServer.model.callServer.serverCode').d('服务编码')
  }, {
    name: 'serverName',
    type: 'string',
    label: intl.get('cmsg.callServer.model.callServer.serverName').d('服务名称')
  }, {
    name: 'serverTypeMeaning',
    type: 'string',
    label: intl.get('cmsg.callServer.model.callServer.serverTypeCode').d('服务类型')
  }, isTenantRoleLevel() && !VERSION_IS_OP && {
    name: 'tenantId',
    type: 'string',
    label: intl.get('cmsg.common.view.source').d('来源')
  }, {
    name: 'enabledFlag',
    type: 'boolean',
    label: intl.get('cmsg.common.view.enabledFlag').d('启用标识'),
    trueValue: 1,
    falseValue: 0,
    defaultValue: 1
  }],
  transport: {
    /**
     * 查询语音电话配置
     */
    read: function read() {
      var url = isTenantRoleLevel() ? "".concat(CPAAS_MSG, "/v1/").concat(organizationId, "/call-servers") : "".concat(CPAAS_MSG, "/v1/call-servers");
      return {
        url: url,
        method: 'GET'
      };
    }
  }
};
var detailDs = {
  autoQueryAfterSubmit: false,
  fields: [!isTenantRoleLevel() && {
    name: 'tenantIdLov',
    type: 'object',
    label: intl.get('entity.tenant.name').d('租户名称'),
    lovCode: 'CPFM.TENANT',
    required: true,
    textField: 'tenantName',
    ignore: 'always',
    noCache: true
  }, !isTenantRoleLevel() && {
    name: 'tenantName',
    type: 'string',
    bind: 'tenantIdLov.tenantName',
    required: true
  }, !isTenantRoleLevel() && {
    name: 'tenantId',
    type: 'string',
    bind: 'tenantIdLov.tenantId',
    required: true
  }, {
    name: 'serverCode',
    type: 'string',
    label: intl.get('cmsg.callServer.model.callServer.serverCode').d('服务编码'),
    required: true,
    maxLength: 30,
    validator: codeValidator
  }, {
    name: 'serverName',
    type: 'string',
    label: intl.get('cmsg.callServer.model.callServer.serverName').d('服务名称'),
    required: true,
    maxLength: 60
  }, {
    name: 'serverTypeCode',
    type: 'string',
    label: intl.get('cmsg.callServer.model.callServer.serverTypeCode').d('服务类型'),
    lookupCode: 'CMSG.CALL.SERVER_TYPE',
    required: true
  }, {
    name: 'accessKey',
    type: 'string',
    label: intl.get('cmsg.callServer.model.callServer.accessKey').d('appId'),
    required: true,
    maxLength: 240
  }, {
    name: 'accessSecret',
    type: 'string',
    label: intl.get('cmsg.callServer.model.callServer.accessSecret').d('密钥')
  }, {
    name: 'enabledFlag',
    type: 'boolean',
    label: intl.get('cmsg.common.view.enabledFlag').d('启用标识'),
    trueValue: 1,
    falseValue: 0,
    defaultValue: 1,
    required: true
  }],
  transport: {
    /**
     * 新建语音电话配置明细
     * @param {Array} data - 参数
     */
    create: function create(_ref) {
      var config = _ref.config,
          data = _ref.data;

      var _data = _slicedToArray(data, 1),
          _data$ = _data[0],
          __id = _data$.__id,
          _status = _data$._status,
          tenantName = _data$.tenantName,
          primaryId = _data$.serverId,
          rest = _objectWithoutProperties(_data$, _excluded);

      var newData = _objectSpread({}, rest);

      var tenantId = newData.tenantId,
          serverCode = newData.serverCode,
          serverName = newData.serverName,
          serverTypeCode = newData.serverTypeCode,
          serverId = newData.serverId,
          accessSecret = newData.accessSecret,
          accessKey = newData.accessKey,
          enabledFlag = newData.enabledFlag,
          objectVersionNumber = newData.objectVersionNumber,
          _token = newData._token,
          tn = newData.tenantName,
          serverTypeMeaning = newData.serverTypeMeaning,
          _st = newData._status,
          extParam = newData.extParam,
          r = _objectWithoutProperties(newData, _excluded2);

      newData.extParam = JSON.stringify(r);
      var url = isTenantRoleLevel() ? "".concat(CPAAS_MSG, "/v1/").concat(organizationId, "/call-servers") : "".concat(CPAAS_MSG, "/v1/call-servers");
      return _objectSpread(_objectSpread({}, config), {}, {
        url: url,
        method: 'POST',
        data: _objectSpread({}, newData)
      });
    },

    /**
     * 更新钉钉配置明细
     * @param {Array} data - 勾选的数据
     * @param {Array} params.permissionList = ['cmsg.ding-talk-config.edit']
     */
    update: function update(_ref2) {
      var config = _ref2.config,
          data = _ref2.data;
      var newData = Array.isArray(data) ? data[0] : {};

      var tenantId = newData.tenantId,
          serverCode = newData.serverCode,
          serverName = newData.serverName,
          serverTypeCode = newData.serverTypeCode,
          serverId = newData.serverId,
          accessSecret = newData.accessSecret,
          accessKey = newData.accessKey,
          enabledFlag = newData.enabledFlag,
          objectVersionNumber = newData.objectVersionNumber,
          _token = newData._token,
          tenantName = newData.tenantName,
          serverTypeMeaning = newData.serverTypeMeaning,
          _status = newData._status,
          extParam = newData.extParam,
          rest = _objectWithoutProperties(newData, _excluded3);

      newData.extParam = JSON.stringify(rest);
      var url = isTenantRoleLevel() ? "".concat(CPAAS_MSG, "/v1/").concat(organizationId, "/call-servers") : "".concat(CPAAS_MSG, "/v1/call-servers");
      return _objectSpread(_objectSpread({}, config), {}, {
        url: url,
        method: 'PUT',
        data: newData
      });
    },

    /**
     * 查询钉钉配置明细
     * @param {Array} data - 勾选的数据
     * @param {Array} params.permissionList = ['cmsg.ding-talk-config.edit']
     */
    read: function read(_ref3) {
      var dataSet = _ref3.dataSet;
      var serverId = dataSet.queryParameter.serverId;
      var url = isTenantRoleLevel() ? "".concat(CPAAS_MSG, "/v1/").concat(organizationId, "/call-servers/").concat(serverId) : "".concat(CPAAS_MSG, "/v1/call-servers/").concat(serverId);
      return {
        url: url,
        method: 'GET',
        params: ''
      };
    }
  }
};
var formConfigDs = {
  transport: {
    /**
     * 表单配置
     */
    read: function read() {
      var url = isTenantRoleLevel() ? "".concat(CPAAS_PLATFORM, "/v1/").concat(organizationId, "/form-lines/header-code") : "".concat(CPAAS_PLATFORM, "/v1/form-lines/header-code");
      return {
        url: url,
        method: 'GET',
        params: {}
      };
    }
  }
};
export { tableDs, detailDs, formConfigDs };